package com.raccoonagents.tools.cascet2d.model.containers;


import java.util.ArrayList;
import java.util.List;

import org.lwjgl.opengl.GL11;

import com.raccoonagents.tools.cascet2d.interfaces.IGraphicContainer;
import com.raccoonagents.tools.cascet2d.interfaces.IMouseTarget;
import com.raccoonagents.tools.cascet2d.interfaces.IRenderableStageElement;
import com.raccoonagents.tools.cascet2d.interfaces.IUpdateable;
import com.raccoonagents.tools.cascet2d.model.Vector2f;
import com.raccoonagents.tools.cascet2d.util.CalculationUtils;
import com.raccoonagents.tools.cascet2d.util.ElementUtils;
import com.raccoonagents.tools.cascet2d.util.MouseManager;
/**
 * all containers for stage are extented from this class.
 * @author Kari Kuvaja
 *
 */
public abstract class AbstractGraphicContainerElement implements IMouseTarget, IGraphicContainer, IUpdateable{
	protected Vector2f size;
	private IRenderableStageElement parent;
	protected Vector2f location;
	protected Vector2f worldCoordinates;
	private final List<IRenderableStageElement> children = new ArrayList<IRenderableStageElement>();
	protected float rotation;
	protected Vector2f pivot = new Vector2f(0.0f,0.0f);
	
	public Vector2f getPivot() {
		return pivot;
	}
	
	@Override
	public final List<IRenderableStageElement> getChildren() {
		return children;
	}
	
	@Override
	public final Vector2f getLocation() {
		return location;
	}

	@Override
	public Vector2f getWorldCoordinates(final Vector2f localCoordinates) {
		return parent.getWorldCoordinates(localCoordinates.add(location));
	}



	public AbstractGraphicContainerElement(final IGraphicContainer parent, final Vector2f location, final Vector2f size){
		this.parent = parent;
		this.location = location;
		this.size = size;
	}

	@Override
	public final boolean isMouseInside() {
		final Vector2f wc = getWorldCoordinates(this.location);
		return CalculationUtils.isPointInside(wc, size, MouseManager.getMouseCoords());
	}

	@Override
	public void render() {
		
		GL11.glPushMatrix();

		GL11.glTranslatef(location.x, location.y, 0);		
		for(final IRenderableStageElement e : children){
			e.render();
		}

		GL11.glPopMatrix();
	}

	
	@Override
	public final void addChild(final IRenderableStageElement e) {
		if(!children.contains(e)){
			children.add(e);			
		}
		e.setParentElement(this);
	}
	
	@Override
	public final void removeChild(final IRenderableStageElement e) {
		children.remove(e);
	}
	
	@Override
	public void setLocation(final Vector2f location) {
		this.location = location;
		
	}
	
	@Override
	public IRenderableStageElement getParentElement() {
		return parent;
	}
	
	
	@Override
	public void setParentElement(final IRenderableStageElement element) {
		this.parent = element;
	}

	@Override
	public void update() {
		ElementUtils.updateContainerRelations(this);		
	}
	/*
	@Override
	public float getWorldRotation(final float rotation) {
		return rotation + this.rotation;
	}*/

	
}